<template>
	<div>
		<!--		查询-->
		<el-row>
			<el-col :span="24">
				<el-form :inline="true" :model="state.query" class="demo-form-inline">
					<el-form-item label="账号">
						<el-input v-model="state.query.userName" placeholder="请输入账号" />
					</el-form-item>
					<el-form-item label="昵称">
						<el-input v-model="state.query.nickName" placeholder="请输入昵称" />
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="init">查找</el-button>
					</el-form-item>
				</el-form>
			</el-col>
		</el-row>
		<!--		按钮-->
		<el-row>
			<el-col :span="24">
				<el-button type="primary" :icon="Edit" @click="btn('新增', {})"
					>新增</el-button
				>
			</el-col>
		</el-row>
		<!--		表单-->
		<el-row>
			<el-col :span="24">
				<el-table :data="state.itemList" stripe style="width: 100%">
					<el-table-column align="center" prop="id" label="编号" />
					<el-table-column align="center" prop="userName" label="账号" />
					<el-table-column align="center" prop="nickName" label="昵称" />
					<el-table-column align="center" prop="profile" label="头像">
						<template #default="scope">
							<el-image
								style="width: 80px; height: 80px"
								:src="scope.row.profile"
							/>
						</template>
					</el-table-column>
					<el-table-column align="center" prop="university" label="毕业学校" />
					<el-table-column align="center" prop="salary" label="您的工资" />
					<el-table-column align="center" prop="post" label="您的爱好" />
					<el-table-column align="center" prop="role" label="角色">
						<template #default="scope">
							<el-tag>{{
								scope.row.role === 1 ? '普通用户' : '管理员'
							}}</el-tag>
						</template>
					</el-table-column>

					<el-table-column
						align="center"
						fixed="right"
						label="操作"
						min-width="120"
					>
						<template #default="scope">
							<el-button
								link
								type="primary"
								size="small"
								@click.prevent="btn('修改', scope.row)"
								>修改</el-button
							>
							<el-button
								link
								type="primary"
								size="small"
								@click.prevent="del(scope.row.id)"
								>删除</el-button
							>
						</template>
					</el-table-column>
				</el-table>
				<div class="page">
					<el-pagination
						background
						layout="prev, pager, next"
						:total="state.query.total"
						@current-change="sendValue"
					/>
				</div>
			</el-col>
		</el-row>
	</div>
	<el-dialog
		v-model="state.dialogVisible"
		:title="state.title"
		width="50%"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<el-form
			ref="ruleFormRef"
			:rules="rules"
			:model="state.from"
			label-width="auto"
			style="max-width: 600px"
		>
			<el-form-item label="账号" prop="userName">
				<el-input v-model="state.from.userName" />
			</el-form-item>
			<el-form-item label="密码" prop="password">
				<el-input show-password v-model="state.from.password" />
			</el-form-item>
			<el-form-item label="昵称" prop="nickName">
				<el-input v-model="state.from.nickName" />
			</el-form-item>
			<el-form-item label="头像" prop="profile">
				<div>
					<el-upload
						class="avatar-uploader"
						action="http://ordinary.jimostudio.link:26451/api/upload?type=10&projectName=anime"
						:show-file-list="false"
						:on-success="handleAvatarSuccess"
					>
						<img
							v-if="state.from.profile"
							:src="state.from.profile"
							class="avatar"
						/>
						<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
					</el-upload>
				</div>
			</el-form-item>
			<el-form-item label="工资" prop="salary">
				<el-input v-model="state.from.salary" />
			</el-form-item>
			<el-form-item label="毕业学校" prop="university">
				<el-input v-model="state.from.university" />
			</el-form-item>
			<el-form-item label="您的爱好" prop="post">
				<el-input v-model="state.from.post" />
			</el-form-item>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button v-if="state.title === '修改'" @click="update(ruleFormRef)"
					>确定 修改</el-button
				>
				<el-button v-else @click="save(ruleFormRef)">确定 新增</el-button>
			</div>
		</template>
	</el-dialog>
</template>
<script setup lang="ts">
import { Edit } from '@element-plus/icons-vue'
import type { UploadProps } from 'element-plus'
import { ref } from 'vue'
const ruleFormRef = ref<FormInstance>()
import type { FormInstance } from 'element-plus'
const rules = reactive({
	userName: [
		{
			required: true,
			message: '账号不能为空',
			trigger: 'change',
		},
	],
	password: [
		{
			required: true,
			message: '密码不能为空',
			trigger: 'change',
		},
	],

	nickName: [
		{
			required: true,
			message: '用户昵称不能为空',
			trigger: 'change',
		},
	],
	salary: [
		{
			required: true,
			message: '工资不能为空',
			trigger: 'change',
		},
	],
	university: [
		{
			required: true,
			message: '毕业学校不能为空',
			trigger: 'change',
		},
	],
	profile: [
		{
			required: true,
			message: '头像不能为空',
			trigger: 'change',
		},
	],
	post: [
		{
			required: true,
			message: '爱好不能为空',
			trigger: 'change',
		},
	],
})
const state = reactive({
	imgPath: '',
	dialogVisible: false,
	title: '',
	//条件查询
	query: {
		userName: '',
		nickName: '',
		page: 1,
		pagesize: 10,
		total: 10000,
	},
	itemList: [],
	from: {},
})
/**
 * 文件上传
 * @param response
 * @param uploadFile
 */
const handleAvatarSuccess: UploadProps['onSuccess'] = (response) => {
	state.from.profile = response.data.path
}
function init() {
	state.imgPath = import.meta.env.VITE_API_BASE_URL + '/file/upload'
	http
		.get(
			`/user/page?page=${state.query.page}&userName=${state.query.userName}&nickName=${state.query.nickName}`,
		)
		.then((res) => {
			state.itemList = res.data.list
			state.query.total = res.data.total
		})
}

//分页
/**
 * 分页出发函数
 * @param val
 */
const sendValue = (val: number) => {
	state.query.page = val
	init()
}

//预打开按钮
function btn(type: string, data: any) {
	state.dialogVisible = true
	state.title = type
	if (type === '修改') {
		state.from = data
		state.from.password = ''
	}
}

//新增
const save = async (formEl: FormInstance | undefined) => {
	if (!formEl) return
	await formEl.validate((valid) => {
		if (valid) {
			http.post('user', state.from).then(() => {
				toast.success('新增成功')
				state.dialogVisible = false
				init()
			})
		}
	})
}
//修改
const update = async (formEl: FormInstance | undefined) => {
	if (!formEl) return
	await formEl.validate((valid) => {
		if (valid) {
			state.from.companyInfo = JSON.stringify(state.from.companyInfo)
			state.from.jobInfo = JSON.stringify(state.from.jobInfo)
			http.put('user/update', state.from).then(() => {
				toast.success('修改成功')
				state.dialogVisible = false
				state.from = {}
				init()
			})
		}
	})
}

//删除
function del(id: number) {
	http.delete(`user/${id}`).then(() => {
		toast.success('删除成功')
		init()
	})
}

init()
</script>

<style scoped>
.page {
	margin: 20px 0;
	display: flex;
	justify-content: center;
	text-align: center;
}
:deep( .avatar-uploader .el-upload ){
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
	transition: var(--el-transition-duration-fast);
}
:deep(.avatar-uploader .el-upload:hover) {
	border-color: var(--el-color-primary);
}
:deep(.el-icon.avatar-uploader-icon) {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
}
</style>
<route lang="json">
{
	"meta": {
		"title": "后台",
		"layout": "admin"
	}
}
</route>
